<template>
  <div class="device">
    <el-page-header @back="goBack" :content="title"> </el-page-header>
    <div class="content">
      <el-form ref="form" :model="form" label-width="120px" label-position="left">
        <el-form-item label="装备模型名称" prop="templateId" :rules="[
          {
            required: true,
            message: '请输入',
            trigger: 'change',
          },
        ]">
          <el-select v-model="form.templateId" placeholder="请选择" style="width: 400px" disabled @change="selectnameOne">
            <el-option v-for="item in templateList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="装备名称" prop="equipmentName" :rules="[
          {
            required: true,
            message: '请输入',
            trigger: 'change',
          },
        ]">
          <el-input class="ipt" v-model="form.equipmentName" disabled></el-input>
        </el-form-item>
        <el-form-item label="装备权重" prop="weight" :rules="[
          {
            required: true,
            message: '请输入',
            trigger: 'change',
          },
        ]">
          <el-input class="ipt" v-model="form.weight" disabled></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-button size="small" type="primary" @click="clickAddRedPlatform">
      添加装备
    </el-button>
    <div class="attribute-content" v-for="(equipment, idx) in equipmentList" :key="idx">
      <el-form ref="form" label-width="120px" label-position="left">
        <div class="table-title">
          <el-form-item label="设备名称">
            <el-input class="ipt" v-model="equipment.equipmentName"></el-input>
          </el-form-item>
          <el-form-item label="权重">
            <el-input v-model="equipment.weight"></el-input>
          </el-form-item>
          <el-button type="text" @click="clickRedPlatformDel(idx)">删除</el-button>
        </div>

        <el-button @click="addAttribute(idx)" type="success" size="small">添加参数</el-button>
        <el-table :data="equipment.attributeList" :height="maxHeight" size="mini" class="table-form">
          <el-table-column label="装备参数名称" prop="attributeName">
            <template slot-scope="scope">
              <el-form-item :rules="[
                {
                  required: true,
                  message: '请输入',
                  trigger: 'blur',
                },
              ]">
                <el-input v-model="scope.row.attributeName" placeholder="请输入装备参数名称"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="参数类型" prop="attributeValueType">
            <template slot-scope="scope">
              <el-form-item :rules="[
                {
                  required: true,
                  message: '请输入',
                  trigger: 'change',
                },
              ]">
                <el-select v-model="scope.row.attributeValueType" placeholder="请选择" style="width: 100%">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="参数值" prop>
            <template slot-scope="scope">
              <el-form-item
                v-if="scope.row.attributeValueType != 1">
                <el-input
                  v-model="scope.row.val"
                  placeholder="请输入值"
                ></el-input>
              </el-form-item>
              <div v-else style="display: flex; align-items: center">
                <el-form-item>
                  <el-input
                    v-model="scope.row.valOne"
                    placeholder="请输入值"
                  ></el-input>
                </el-form-item>
                <span>~</span>
                <el-form-item>
                  <el-input
                    v-model="scope.row.valTwo"
                    placeholder="请输入值"
                  ></el-input>
                </el-form-item>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unit">
            <template slot-scope="scope">
              <el-form-item :rules="[
                {
                  required: true,
                  message: '请输入',
                  trigger: 'blur',
                },
              ]">
                <el-input v-model="scope.row.unit" placeholder="请输入单位"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" prop width="64">
            <template slot-scope="scope">
              <el-button type="text" @click="clickDelRedEquip(scope, idx)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <div class="btn">
      <el-button type="primary" @click="changeSure">确定</el-button>
      <el-button @click="goBack">取消</el-button>
    </div>
  </div>
</template>
<script>
import {
  getEquipList,
  getTemplateDetail,
  editEquipment,
  getEquipDetail,
  getRedEquipDetail,
  editRedEquipment,
} from "@/api/equip";
export default {
  props: {
    detailList: {
      type: Object,
      default: () => { },
    },
    type: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      maxHeight: "calc(100vh - 444px)",
      title: "编辑装备",
      form: {
        templateId: "",
        equipmentName: "",
        weight: "",
        tableData: [],
      },
      options: [
        {
          value: 0,
          label: "值",
        },
        {
          value: 1,
          label: "范围",
        },
      ],
      equipmentList: [
        {
          equipmentName: "",
          weight: 1,
          attributeList: [
            {
              attributeName: "",
              attributeValueType: "",
              values: [],
              valOne: "",
              valTwo: "",
              val: "",
              unit: "",
            },
          ],
        },
      ],
      deleteAttributeList: [],
      deleteEquipmentList: [],
      templateList: [],
      list: [],
    };
  },
  mounted() {
    this.templateList = [];
    getEquipList({ type: this.type, belong: 1 })
      .then((res) => {
        const { code, data } = res?.data;
        if (code == 200) {
          data.forEach((item) => {
            this.templateList.push({
              label: item.templateName,
              value: item.templateId,
            });
          });
        }
        this.form.templateId = this.detailList.templateId;
      })
      .catch((err) => {
        this.$message.error(err);
      });
    this.form.equipmentName = this.detailList.equipmentName;
    this.form.weight = this.detailList.weight;
    getRedEquipDetail({ equipmentId: this.detailList.equipmentId })
      .then((res) => {
        const { code, data, msg } = res?.data;
        if (code == 200) {
          data.forEach((item) => {
            item.attributeList.forEach((t) => {
              var arr = t.values;
              if (t.attributeValueType == 1) {
                t.valOne = arr[0];
                t.valTwo = arr[1];
              } else {
                t.val = arr[0];
              }
            })
          })
          this.equipmentList = data;
        } else {
          this.$message.error(msg);
        }
      })
      .catch((err) => {
        this.$message.error(err);
      });
  },
  methods: {
    // 添加装备
    clickAddRedPlatform() {
      this.equipmentList.push({
        equipmentName: "",
        weight: 1,
        attributeList: [
          {
            attributeName: "",
            attributeValueType: 0,
            val: "",
            unit: "",
          },
        ],
      });
    },
    // 删除装备
    clickRedPlatformDel(index) {
      this.equipmentList[index].operation = "delete";
      this.deleteEquipmentList.push(this.equipmentList[index]);
      this.equipmentList.splice(index, 1);
    },
    // 删除装备参数
    clickDelRedEquip(row, index) {
      this.equipmentList[index].attributeList.splice(row.$index, 1);
    },
    //添加装备参数
    addAttribute(index) {
      this.equipmentList[index].attributeList.push(
        {
          attributeName: "",
          attributeValueType: 0,
          val: "",
          unit: "",
        }
      );
    },
    selectnameOne(val) {
      getTemplateDetail({ templateId: val })
        .then((res) => {
          const { code, data, msg } = res?.data;
          if (code == 200) {
            data.forEach((item) => {
              this.form.tableData.push({
                attributeName: item.attributeName,
                unit: item.unit,
                attributeValueType: this.attributeValueType(
                  item.attributeValueType
                ),
                num: item.values?.lenght == 1 ? item.values[0] : "",
                valOne: item.values?.lenght == 2 ? item.values[0] : "",
                valTwo: item.values?.lenght == 2 ? item.values[1] : "",
                attributeId: item.attributeId,
              });
            });
          } else {
            this.$message.error(msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    attributeValueType(val) {
      switch (val) {
        case "NUMERIC":
          return 0;
        case "RANGE":
          return 1;
        default:
          return "";
      }
    },
    goBack() {
      this.$emit("goBack");
    },
    changeSure() {
      let a = this.equipmentList.concat(this.deleteEquipmentList);
      a.forEach((item) => {
        item.templateId = this.form.templateId;
        item.equipmentId = this.detailList.equipmentId;
        item.attributeList.forEach(a => {
          var arr = [];
          if(a.attributeValueType == 1){
            arr[0] = a.valOne;
            arr[1] = a.valTwo;
          }else{
            arr[0] = a.val;
          }
          a.values = arr;
        })
      });
      editRedEquipment(a)
        .then((res) => {
          const { code, msg } = res?.data;
          if (code == 200) {
            this.$message.success(msg);
            this.$emit("changeEdit");
          } else {
            this.$message.error(msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    handleData(data) {
      data.forEach((item) => {
        if (item.attributeValueType == 0) {
          this.list.push({
            attributeName: item.unit,
            unit: item.unit,
            attributeValueType: item.attributeValueType,
            values: [item.num],
            attributeId: item.attributeId,
          });
        } else {
          this.list.push({
            attributeName: item.unit,
            unit: item.unit,
            attributeValueType: item.attributeValueType,
            values: [item.valOne, item.valTwo],
            attributeId: item.attributeId,
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.btn {
  margin-top: 24px;
  text-align: right;
}

.content {
  margin-top: 24px;
}

.attribute-content {
  background: #e4e7ed;
  padding: 24px;
  border-radius: 4px;
  margin: 8px 0 16px 0px;
}

.table-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

:deep(.el-input).ipt {
  width: 400px;
}
</style>
